<?php

namespace Addons\message;
use Common\Controller\Addon;

/**
 * 站内信插件
 * @author 无名
 */

    class messageAddon extends Addon{

        public $info = array(
            'name'=>'message',
            'title'=>'站内信',
            'description'=>'站内信',
            'status'=>1,
            'author'=>'无名',
            'version'=>'0.1'
        );

        public function install(){
            return true;
        }

        public function uninstall(){
            return true;
        }

        // 实现的messageInstation钩子方法
        public function messageInstation($param){

        }

        public function add(){
            $model = M('Model')->where(array('status' => 1))->find("42");
            $model || $this->error('模型不存在！');
            if(IS_POST){
                $Model  =   D(parse_name(get_table_name($model['id']),1));
                // 获取模型的字段信息
                $Model  =   $this->checkAttr($Model,$model['id']);
                $data=$Model->create();
                
                //收件人ID     member_id
                if($data){
                    $message_id=$Model->add();
                }
                if($message_id){
                    switch ($data['send_type']) {
                    case 0://所有用户
                        $Member=M('Member')->getField('uid',true);
                        foreach ($Member as $member_id) {
                            M("msgcon")->add(array("read_status"=>0,'member_id'=>$member_id,'message_id'=>$message_id));
                        }
                        break;
                    case 1://指定分组
                        $Member=M("auth_group_access")->where(array("group_id"=>825))->getField("uid",true);
                        $datalist=array();

                        foreach ($Member as $member_id) {
                           $datalist[]=array("read_status"=>0,'member_id'=>$member_id,'message_id'=>$message_id);
                        }
                        M("msgcon")->addAll($datalist);
                        break;
                    case 2://按代理地区指定代理商
                        $member_id=M('Member')->where(array("agent_category_id"=>I("post.agent_category_id")))->getField('uid');
                        M("msgcon")->add(array("read_status"=>0,'member_id'=>$member_id,'message_id'=>$message_id));
                        break;
                    
                    default:
                        # code...
                        break;
                    }
                    
                    $this->success('添加'.$model['title'].'成功！' , U('lists?model='.$model['name']));
                } else {
                    $this->error($Model->getError());
                }
            } else {

                $fields = get_model_attribute($model['id']);

                $this->assign('model', $model);
                $this->assign('fields', $fields);
                $this->meta_title = '新增'.$model['title'];
                $this->display($model['template_add']?$model['template_add']:'');
            }
        }

    }